package com.huike.game.Activity;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.huike.game.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import androidx.appcompat.app.AppCompatActivity;

public class GameActivity14 extends AppCompatActivity implements View.OnClickListener {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private TextView tv_game14_time;
    private TextView tv_game14_number;
    private TextView tv_game14_number1;
    private Switch sh_game14_1;
    private TextView tv_game14_number2;
    private Switch sh_game14_2;
    private TextView tv_game14_number3;
    private Switch sh_game14_3;
    private ImageView iv_game14_red_green_light;
    private AnimationDrawable animationDrawable;

    private Boolean isClick1 = false;
    private Boolean isClick2 = false;
    private Boolean isClick3 = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game14);
        initView();


        SimpleDateFormat sd = new SimpleDateFormat("yyyy年MM月dd日");
        String format = sd.format(new Date(new Date().getTime()));

//        初始时间
        initTime(format);

        initClick();

    }



    private void initClick() {
        sh_game14_1.setOnClickListener(this);
        sh_game14_2.setOnClickListener(this);
        sh_game14_3.setOnClickListener(this);
        tv_game14_time.setOnClickListener(this);
    }

    @SuppressLint("SimpleDateFormat")
    private void initTime(String format) {
        tv_game14_time.setText(format);

//        开关判断
        String num = format.substring(format.lastIndexOf("日") - 1, format.lastIndexOf("日"));
        if(Integer.parseInt(num) % 2 != 0){//单
            tv_game14_number.setText("单号出行车辆：1、3");

            isClick1 = true;
            isClick3 = true;
            isClick2 = false;

            sh_game14_1.setChecked(true);
            sh_game14_2.setChecked(false);
            sh_game14_3.setChecked(true);

            sh_game14_1.setEnabled(true);
            sh_game14_2.setEnabled(false);
            sh_game14_3.setEnabled(true);


        }else{
            tv_game14_number.setText("双号出行车辆：2");

            isClick1 = false;
            isClick3 = false;
            isClick2 = true;

            sh_game14_1.setChecked(false);
            sh_game14_2.setChecked(true);
            sh_game14_3.setChecked(false);

            sh_game14_1.setEnabled(false);
            sh_game14_2.setEnabled(true);
            sh_game14_3.setEnabled(false);

        }

    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);
        tv_game14_time = (TextView) findViewById(R.id.tv_game14_time);
        tv_game14_number = (TextView) findViewById(R.id.tv_game14_number);
        tv_game14_number1 = (TextView) findViewById(R.id.tv_game14_number1);
        sh_game14_1 = (Switch) findViewById(R.id.sh_game14_1);
        tv_game14_number2 = (TextView) findViewById(R.id.tv_game14_number2);
        sh_game14_2 = (Switch) findViewById(R.id.sh_game14_2);
        tv_game14_number3 = (TextView) findViewById(R.id.tv_game14_number3);
        sh_game14_3 = (Switch) findViewById(R.id.sh_game14_3);
        iv_game14_red_green_light =  findViewById(R.id.iv_game14_red_green_light);

         animationDrawable = (AnimationDrawable) iv_game14_red_green_light.getBackground();
         animationDrawable.start();

        iv_rigth_image.setVisibility(View.GONE);
        tv_right_text.setVisibility(View.GONE);

        tv_content_title.setText("出行管理");

        iv_left_image.setImageResource(R.drawable.tiao);
        tv_left_text.setVisibility(View.GONE);


    }

//    开关点击
    @Override
    public void onClick(View view) {
        if(view == sh_game14_1){
            isClick1 = !isClick1;
            sh_game14_1.setChecked(isClick1);
            if(isClick1){
                tv_game14_number1.setText("开");
            }else{
                tv_game14_number1.setText("停");
            }
        }else  if(view == sh_game14_2){
            isClick2 = !isClick2;
            sh_game14_2.setChecked(isClick2);
            if(isClick2){
                tv_game14_number2.setText("开");
            }else{
                tv_game14_number2.setText("停");

            }
        }else  if (view == sh_game14_3){
            isClick3 = !isClick3;
            sh_game14_3.setChecked(isClick3);
            if(isClick3){
                tv_game14_number3.setText("开");
            }else{
                tv_game14_number3.setText("停");
            }

        }else if(view == tv_game14_time){//时间点击

            Calendar calendar = Calendar.getInstance();

            DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                    int year = datePicker.getYear();
                    int month = datePicker.getMonth() + 1;
                    int dayOfMonth = datePicker.getDayOfMonth();
                    String data = year + "年"+month+"月"+dayOfMonth+"日";

                    initTime(data);

                }
            }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

            dialog.show();

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        animationDrawable.stop();
        animationDrawable = null;

    }
}
